package com.ytjj.qmyx.admin.controller;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.model.YxAlipayConfig;
import com.ytjj.common.model.YxDomain;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.SysConfigRequest;
import com.ytjj.qmyx.admin.model.response.ShortChainConfigResponse;
import com.ytjj.qmyx.admin.model.response.SysConfigResponse;
import com.ytjj.qmyx.admin.service.SysconfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sysconfig")
public class SysConfigController {

    @Autowired
    private SysconfigService sysconfigService;

    @Resource
    private SysConfigUtil sysConfigUtil;

    @RequiresPermissions("sysconfig:list")
    @PostMapping("/list")
    @OperLog(operModule = "系统配置模块", operDesc = "查询系统配置", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult list(@RequestBody SysConfigRequest sysConfigRequest){
        CommonPage<SysConfigResponse> pageList = sysconfigService.querySysConfigList(sysConfigRequest);
        return CommonResult.success(pageList);
    }

    @RequiresPermissions("sysconfig:insertSysConfig")
    @PostMapping("/insertSysConfig")
    @OperLog(operModule = "系统配置模块", operDesc = "添加系统配置", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertSysConfig(@RequestBody SysConfigRequest sysConfigRequest){
        int count = sysconfigService.insertSysConfig(sysConfigRequest);
        if(count > 0){
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @RequiresPermissions("sysconfig:updateSysConfig")
    @PostMapping("/updateSysConfig")
    @OperLog(operModule = "系统配置模块", operDesc = "修改系统配置", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateSysConfig(@RequestBody SysConfigRequest sysConfigRequest){
        int count = sysconfigService.updateSysConfig(sysConfigRequest);
        if(count > 0){
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 获取短链配置域名
     */
    @GetMapping("/shortChainRealmList")
    public CommonResult shortChainRealmList(){
        List<ShortChainConfigResponse> list = sysconfigService.shortChainRealmList();
        return CommonResult.success(list);
    }

    /**
     * 获取指定配置
     * @param key
     * @return
     */
    @GetMapping("/getSysConfig")
    public CommonResult getSysConfig(@RequestParam("key") String key) {
        return CommonResult.success(sysconfigService.getSysConfig(key));
    }

    /**
     * 获取指定配置全部信息
     * @param key
     * @return
     */
    @GetMapping("/getSysConfigInfo")
    public CommonResult<SysConfigResponse> getSysConfigInfo(@RequestParam("key") String key) {
        return CommonResult.success(sysconfigService.getSysConfigInfo(key));
    }

    /**
     * 获取微信-支付宝开关配置
     */
    @GetMapping("/getPaySysConfig")
    public CommonResult<List<SysConfigResponse>> getPaySysConfig() {
        return CommonResult.success(sysconfigService.getPaySysConfig());
    }

    /**
     * 支付宝配置
     * @return
     */
    @GetMapping("/getAlipayConfig")
    public CommonResult<List<YxAlipayConfig>> getAlipayConfig() {
        return CommonResult.success(sysconfigService.getAlipayConfig());
    }

    /**
     * 修改支付宝配置
     * @param id
     * @return
     */
    @GetMapping("/updateAlipayConfig")
    public CommonResult updateAlipayConfig(Integer id){
        Assert.notNull(id,"id不能为空");
        sysconfigService.updateAlipayConfig(id);
        return CommonResult.success();
    }

    /**
     * 获取切换域名列表
     * @return
     */
    @GetMapping("/getDomainUrl")
    public CommonResult<List<YxDomain>> getDomainUrl() {
        return CommonResult.success(sysconfigService.getDomainUrl());
    }

    /**
     * 切换短域名对应的长域名
     * @param id
     * @return
     */
    @GetMapping("/updateDomainUrl")
    public CommonResult updateDomainUrl(Integer id){
        Assert.notNull(id,"id不能为空");
        sysconfigService.updateDomainUrl(id);
        return CommonResult.success();
    }

    /**
     * 获取支付宝账号
     * @return
     */
    @GetMapping("/getAlipayAccount")
    public CommonResult getAlipayAccount() {
        return CommonResult.success(sysconfigService.getAlipayAccount());
    }

}
